<?php
/**
* A DHTML menu component for Joomla
* @version 1.6.3
* @package ngmenu
* @copyright (C) 2005 - 2012 by Georg Lorenz
* @license Released under the terms of the GNU General Public License
**/

/** ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Restricted access' );

class MenuPreview {
	var $config = array();
	var $items;
//	var $ng_ids=array();
	var $sub_idx=0;
		
	function MenuPreview($menu, $items){
		$this->config['menu'] = $menu;
		$this->items = $items;
	}
		
	function create(){
		$document =& JFactory::getDocument();
		
		$build_menu = '';
		if(count($this->items) && count($this->config['menu'])){
			$build_menu = $this->_build_seo_items();
			$build_menu .= "\n<script type=\"text/javascript\">\n";
			$build_menu .= $this->_build_menu_vars();
//			$build_menu .= $this->_build_menu_items();
//			$build_menu .= 'make_preview();';
			$build_menu .= "make_preview('".JText::_('COM_NGMENU_PREVIEW')."', 'menu_preview');";
			
			$build_menu .= "\n</script>";
		}
		
		$font_parm = NGMenuHelper::getGoogleFontParms($this->config['menu']->font_family, $this->config['menu']->font_weight);
		if(!empty($font_parm)){
			$document->addStyleSheet( 'http://fonts.googleapis.com/css?family='.$font_parm );
		}

		return $build_menu;
	}
		
	function _build_menu_items(){

		$mosConfig_live_site = JURI::Root().'//';
		$mosConfig_live_site = str_replace( '///', '', $mosConfig_live_site );

		$menu_items = array();

		foreach($this->items as $item_id => $item){

			$is_separator = $item->is_separator;
			if($is_separator){
				if(substr_count($item->label, '-') >= strlen($item->label)/2){
					$item_label = '-';
				}else{
					$is_separator = 0;
					$item_label = addslashes($item->label);
				}
			}else{
				$item_label = addslashes($item->label);
			}


			if($is_separator){
				$menu_items[$item->parent_id][$item_id] = $item_label."', '', , , , , , ,]";
			}else{
				$menu_items[$item->parent_id][$item_id] = $item_label."', 'javascript:open_ngitemform();', , , '".JText::_('COM_NGMENU_EDIT_ITEM_PROPERTIES')."', , , ,]";
			}
		}

		$menu = $this->_get_sub_menu_items($menu_items);
		return "var menuItems = [\n".$menu."\n];";
	}
		
	function _get_sub_menu_items($menu_items, $parent_id=1, $sub_level=0){
		$str = '';
		$item_idx = 0;
		$sub_level = ($parent_id > 1) ? $sub_level + 1 : 0;

		if(array_key_exists($parent_id, $menu_items)){
			if(is_array($menu_items[$parent_id])){
				$this->sub_idx = ($sub_level) ? $this->sub_idx + 1 : $this->sub_idx;
				foreach($menu_items[$parent_id] as $item_id => $value){
					$sub_idx = ($sub_level) ? $this->sub_idx : $sub_level;
//					$this->ng_ids[$item_id] = 'm'.$sub_idx.'i'.$item_idx.'tbl';

					$str .= "\n['".str_repeat("|", $sub_level).$value;
					if($sub_menus = $this->_get_sub_menu_items($menu_items, $item_id, $sub_level)){
						$str .= ",\n".$sub_menus;
					}else{
						$str .= ",\n";
					}
					$item_idx += 1;
				}
			}
		}
		return $str;
	}

	function _build_seo_items(){
		
		$menu_items = array();
		
		foreach($this->items as $item){
			foreach($item as $field => $value){
				$menu_items[$item->parent_id][$item->item_id][$field] = $value;
			}
		}
		return "\n<ul id=\"ng".$this->config['menu']->id."\">".$this->_get_seo_items($menu_items)."\n</ul>";
	}

	function _get_seo_items($menu_items, $parent_id=1){

		$str = $icon_normal = $icon_over = '';
		$img_normal = $img_over = '';

		if(array_key_exists($parent_id, $menu_items)){
			if(is_array($menu_items[$parent_id])){
				foreach($menu_items[$parent_id] as $item_id => $item){
					if ($item['default_icon']){
						$icon_normal = $item['icon_normal'];
						$icon_over = $item['icon_over'];
					}
					if (!empty($item['icon_normal'])){
						$img_normal = "<img class=\"def\" src=\"".JURI::root(true).$item['icon_normal']."\" alt=\"".$item['label']."\"/>";
					}elseif(!empty($icon_normal)){
						$img_normal = "<img class=\"def\" src=\"".JURI::root(true).$icon_normal."\" alt=\"".$item['label']."\"/>";
					}
					if (!empty($item['icon_over'])){
						$img_over = "<img class=\"over\" src=\"".JURI::root(true).$item['icon_over']."\" alt=\"".$item['label']."\"/>";
					}elseif(!empty($icon_over)){
						$img_over = "<img class=\"over\" src=\"".JURI::root(true).$icon_over."\" alt=\"".$item['label']."\"/>";
					}
					
					$link = "javascript:open_ngitemform()";
					if($sub_menus = $this->_get_seo_items($menu_items, $item_id)){
						$str .= "\n<li><a href=\"".$link."\" target=\"_nav\"><span>".$img_normal.$img_over.$item['label']."</span></a>";
						$str .= "\n<ul>";
						$str .= $sub_menus;
						$str .= "\n</ul>";
						$str .= "\n</li>";
					}else{
						if($item['is_separator']){
							$str .= "\n<li class=\"sep\"><img src=\"".JURI::root(true).$this->config['menu']->separator_vimage."\" alt=\"\"/></li>";
						}else{
							$str .= "\n<li><a href=\"".$link."\" target=\"_nav\">".$img_normal.$img_over.$item['label']."</a></li>";
						}
					}
					$img_normal = $img_over = '';
				}
			}
		}
		return $str;
	}

	function _build_menu_vars(){

		$mosConfig_live_site = JURI::Root();
		
		$vars = 'var menuIdentifier="ng'.$this->config['menu']->id.'";'."\n";
		$vars .= 'var isHorizontal='.$this->config['menu']->is_horizontal.';'."\n";
		$vars .= 'var pathPrefix_img="'.$this->config['menu']->path_prefix_img.'";'."\n";
		$vars .= 'var pathPrefix_link="'.$this->config['menu']->path_prefix_link.'";'."\n";
		$vars .= 'var saveNavigationPath='.$this->config['menu']->save_navigation_path.';'."\n";
		$vars .= 'var showByClick='.$this->config['menu']->show_by_click.';'."\n";
		$vars .= 'var pressedItem=-1;'."\n";
		$vars .= 'var blankImage="'.$mosConfig_live_site.$this->config['menu']->blank_image.'";'."\n";
		$vars .= 'var noWrap='.$this->config['menu']->no_wrap.';'."\n";
		$vars .= 'var dm_writeAll=1;'."\n";
		$vars .= 'var smHidePause='.$this->config['menu']->sm_hide_pause.';'."\n";
		$vars .= 'var smShowPause='.$this->config['menu']->sm_show_pause.';'."\n";
		$vars .= 'var smColumns=1;'."\n";
		$vars .= 'var itemCursor="'.$this->config['menu']->item_cursor.'";'."\n";
		$vars .= 'var itemTarget="'.$this->config['menu']->item_target.'";'."\n";
		$vars .= 'var statusString="'.$this->config['menu']->status_string.'";'."\n";
		if(empty($this->config['menu']->menu_width)){
			$vars .= 'var menuWidth="1px";'."\n";
		}elseif($this->config['menu']->menu_width_unit == '%'){
			$vars .= 'var menuWidth="170px";'."\n";
		}else{
			$vars .= 'var menuWidth="'.$this->config['menu']->menu_width.$this->config['menu']->menu_width_unit.'";'."\n";
		}
		if($this->config['menu']->is_horizontal){
			$menu_height = !empty($this->config['menu']->menu_height) ? $this->config['menu']->menu_height.'px";' : '";';
			$vars .= 'var menuHeight="'.$menu_height."\n";
		}else{
			$vars .= 'var menuHeight="";'."\n";
		}
		$vars .= 'var arrowWidth='.$this->config['menu']->arrow_width.';'."\n";
		$vars .= 'var arrowHeight='.$this->config['menu']->arrow_height.';'."\n";
		$vars .= 'var absolutePos=0;'."\n";
		$color = (empty($this->config['menu']->menu_back_color)) ? 'transparent' : $this->config['menu']->menu_back_color;
		$vars .= 'var menuBackColor="'.$color.'";'."\n";
		$vars .= 'var menuBorderStyle="'.$this->config['menu']->menu_border_style.'";'."\n";
		$vars .= 'var menuBorderColor="'.$this->config['menu']->menu_border_color.'";'."\n";
		$vars .= 'var menuBorderWidth="'.$this->config['menu']->menu_border_width.'px";'."\n";
		$color_normal = (empty($this->config['menu']->item_back_color_normal)) ? 'transparent' : $this->config['menu']->item_back_color_normal;
		$color_over = (empty($this->config['menu']->item_back_color_over)) ? 'transparent' : $this->config['menu']->item_back_color_over;
		$vars .= 'var itemBackColor=["'.$color_normal.'","'.$color_over.'"];'."\n";
		$vars .= 'var itemBorderStyle=["'.$this->config['menu']->item_border_style_normal.'","'.$this->config['menu']->item_border_style_over.'"];'."\n";
		$vars .= 'var itemBorderWidth="'.$this->config['menu']->item_border_width.'px";'."\n";
		$color_normal = $this->config['menu']->item_border_color_normal;
		$color_over = $this->config['menu']->item_border_color_over;
		$vars .= 'var itemBorderColor=["'.$color_normal.'","'.$color_over.'"];'."\n";
		$vars .= 'var itemAlignTop="'.$this->config['menu']->item_align_main.'";'."\n";
		$vars .= 'var itemAlign="'.$this->config['menu']->item_align_main.'";'."\n";
		$vars .= 'var itemSpacing='.$this->config['menu']->item_spacing.';'."\n";
		$vars .= 'var itemPadding="'.$this->config['menu']->item_padding_top.'px '.$this->config['menu']->item_padding_right.'px '.$this->config['menu']->item_padding_bottom.'px '.$this->config['menu']->item_padding_left.'px";'."\n";
		$font_normal = NGMenuHelper::prepareFontWeight($this->config['menu']->font_weight, true).' '.$this->config['menu']->font_size.$this->config['menu']->font_size_unit.' '.NGMenuHelper::prepareFontFamily($this->config['menu']->font_family);
		$font_over = NGMenuHelper::prepareFontWeight($this->config['menu']->font_weight, true).' '.$this->config['menu']->font_size.$this->config['menu']->font_size_unit.' '.NGMenuHelper::prepareFontFamily($this->config['menu']->font_family);
		$vars .= 'var fontStyle=["'.$font_normal.'","'.$font_over.'"];'."\n";
		$vars .= 'var fontColor=["'.$this->config['menu']->font_color_normal.'","'.$this->config['menu']->font_color_over.'"];'."\n";
		$vars .= 'var fontDecoration=["'.$this->config['menu']->font_decoration_normal.'","'.$this->config['menu']->font_decoration_over.'"];'."\n";
		$vars .= 'var fontColorDisabled="#AAA";'."\n";
		if($this->config['menu']->main_separator_create){
			$vars .= 'var separatorVImage="'.$mosConfig_live_site.$this->config['menu']->separator_vimage.'";'."\n";
		}else{
			$vars .= 'var separatorVImage="";'."\n";
		}
		$vars .= 'var separatorImage="";'."\n";
		$vars .= 'var separatorWidth="'.$this->config['menu']->separator_hwidth.$this->config['menu']->separator_hwidth_unit.'";'."\n";
		$vars .= 'var separatorHeight="'.$this->config['menu']->separator_hheight.'px";'."\n";
		$vars .= 'var separatorAlignment="'.$this->config['menu']->separator_halign.'";'."\n";
		$vars .= 'var separatorVWidth="'.$this->config['menu']->separator_vwidth.'px";'."\n";
		$vars .= 'var separatorVHeight="'.$this->config['menu']->separator_vheight.$this->config['menu']->separator_vheight_unit.'";'."\n";
		if($this->config['menu']->main_arrow_image_create){
			$vars .= 'var arrowImageMain=["'.$mosConfig_live_site.$this->config['menu']->main_arrow_image_normal.'","'.$mosConfig_live_site.$this->config['menu']->main_arrow_image_over.'"];'."\n";
		}else{
			$vars .= 'var arrowImageMain=["",""];'."\n";
		}
		$vars .= 'var arrowImageSub=["",""];'."\n";
		if($this->config['menu']->menu_back_image_create){
			$vars .= 'var menuBackImage="'.$mosConfig_live_site.$this->config['menu']->menu_back_image.'";'."\n";
		}else{
			$vars .= 'var menuBackImage="";'."\n";
		}
		$vars .= 'var menuBackRepeat="'.$this->config['menu']->menu_back_image_repeat.'";'."\n";
		if($this->config['menu']->item_back_image_create){
			$vars .= 'var itemBackImage=["'.$mosConfig_live_site.$this->config['menu']->item_back_image_normal.'","'.$mosConfig_live_site.$this->config['menu']->item_back_image_over.'"];'."\n";
		}else{
			$vars .= 'var itemBackImage=["",""];'."\n";
		}
		if($this->config['menu']->effect_transparency_create){
			$vars .= 'var transparency="'.$this->config['menu']->effect_transparency.'";'."\n";
		}else{
			$vars .= 'var transparency="100";'."\n";
		}
		if(!$this->config['menu']->effect_transition_create){
			$vars .= 'var transition=-1;'."\n";
		}
		$vars .= 'var transDuration=400;'."\n";
		$vars .= 'var transDuration2=400;'."\n";
		$vars .= 'var dmObjectsCheck=1;'."\n";
		$vars .= 'var dynamic=1;'."\n";
		$vars .= 'var subMenuAlign="left";'."\n";
		$vars .= 'var subMenuVAlign="top";'."\n";
		$vars .= 'var smSmartScroll=1;'."\n";
		$vars .= 'var smHideOnClick=0;'."\n";
		$vars .= 'var beforeItemImage=["",""];'."\n";
		$vars .= 'var afterItemImage=["",""];'."\n";
		$vars .= 'var beforeItemImageH="";'."\n";
		$vars .= 'var afterItemImageH="";'."\n";
		$vars .= 'var beforeItemImageW="";'."\n";
		$vars .= 'var afterItemImageW="";'."\n";
		$vars .= 'var arrowHeightSub=0;'."\n";
		$vars .= 'var arrowWidthSub=0;'."\n";
		$vars .= 'var itemSlideBack=0;'."\n";
		$vars .= 'var separatorColor="";'."\n";
		$vars .= 'var separatorVColor="";'."\n";
		$vars .= 'var smFrameImage="";'."\n";
		$vars .= 'var smFrameWidth=0;'."\n";
		
		return $vars;
	}

}
?>
